package com.open.capacity.points.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.common.exception.service.ServiceException;
import com.open.capacity.common.model.PointsLog;
import com.open.capacity.common.util.PageUtil;
import com.open.capacity.common.web.CodeEnum;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.points.dao.PointsLogDao;
import com.open.capacity.points.dto.PointsLogDto;
import com.open.capacity.points.service.PointsLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class PointsLogServiceImpl extends ServiceImpl<PointsLogDao, PointsLog> implements PointsLogService {
    @Override
    public PageResult<PointsLogDto> findPointsLogByCondition(Map<String, Object> cond) throws ServiceException {
        try {
            PageHelper.startPage(MapUtils.getInteger(cond, PageUtil.PAGE), MapUtils.getInteger(cond, PageUtil.LIMIT));
            List<PointsLogDto> pointsLogDtoList = baseMapper.selectPointsLogByCondition(cond);
            PageInfo<PointsLogDto> pageInfo = new PageInfo<>(pointsLogDtoList);
            return PageResult.<PointsLogDto>builder().code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).data(pageInfo.getList()).build();
        } catch (Exception e) {
            log.debug(e.getMessage());
            throw new ServiceException(e);
        }
    }
}
